//
//  KeyWindowView.m
//  sdk2.0zhengquandasha
//
//  Created by Choi on 16/1/4.
//  Copyright © 2016年 palmaplus. All rights reserved.
//

#import "KeyWindowView.h"

@implementation KeyWindowView
-(instancetype)init{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT);
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
        self.tag = 10001;
//        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
//        backView.backgroundColor = [UIColor blackColor];
//        backView.alpha = 0.8;
//        [self addSubview:backView];
        UIWindow *temp = [UIApplication sharedApplication].keyWindow;
        if (temp) {
            [temp addSubview:self];
        }else{
            NSLog(@"keyWindow还没有被创建");
        }
    }
    return self;
}

static KeyWindowView *_keyWindowView;
+(instancetype)sharedKeyWindowView{
    static  dispatch_once_t once;
    dispatch_once (&once, ^{
        _keyWindowView = [[KeyWindowView alloc]init];
//        [_keyWindowView addObserver:_keyWindowView forKeyPath:@"str" options:NSKeyValueObservingOptionNew context:nil];
    });
    if ([[UIApplication sharedApplication].keyWindow viewWithTag:10001]) {
        
    }else{
        [[UIApplication sharedApplication].keyWindow addSubview:_keyWindowView];
    }
    //监听它的子视图
    _keyWindowView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
    return _keyWindowView;
}



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [KeyWindowView removeFromSuperView:0.5];
 
}

+(void)removeFromSuperView:(NSTimeInterval)time{
    [_keyWindowView removeFromSuperview:time];
}

-(void)removeFromSuperview:(NSTimeInterval)time{
    for (UIView *tempView in [KeyWindowView sharedKeyWindowView].subviews) {
        NSString * p =   [NSString stringWithFormat:@"%@",NSStringFromClass(tempView.class)];
        NSDictionary *dic = [_keyWindowView.dicSubviews objectForKey:p];
        [UIView animateWithDuration:time animations:^{
            tempView.frame = [[dic objectForKey:@"frame"] CGRectValue];
            tempView.alpha = [[dic objectForKey:@"alpha"] floatValue];
            _keyWindowView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
        } completion:^(BOOL finished) {
            [_keyWindowView.dicSubviews removeObjectForKey:p];
            [tempView removeFromSuperview];
            [_keyWindowView removeFromSuperview];
        }];
    }
}

-(void)removeFromSuperview{
    if (self.cancelBlock) {
        self.cancelBlock();
    }
    [super removeFromSuperview];
//    for (UIView *tempView in [KeyWindowView sharedKeyWindowView].subviews) {
//        NSString * p =   [NSString stringWithFormat:@"%@",NSStringFromClass(tempView.class)];
//        NSDictionary *dic = [_keyWindowView.dicSubviews objectForKey:p];
//        [UIView animateWithDuration:0.3 animations:^{
//            tempView.frame = [[dic objectForKey:@"frame"] CGRectValue];
//            tempView.alpha = [[dic objectForKey:@"alpha"] floatValue];
//            _keyWindowView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
//        } completion:^(BOOL finished) {
//            [_keyWindowView.dicSubviews removeObjectForKey:p];
//            [tempView removeFromSuperview];
//            [super removeFromSuperview];
//            if (self.cancelBlock) {
//                self.cancelBlock();
//            }
//        }];
//    }
}


+(void)showWithAnimation:(NSTimeInterval)time withAlpha:(CGFloat)alpha{
    if ([[UIApplication sharedApplication].keyWindow viewWithTag:10001]) {
        _keyWindowView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        [[UIApplication sharedApplication].keyWindow addSubview:_keyWindowView];
        [UIView animateWithDuration:time animations:^{
            _keyWindowView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:alpha];
        }];
    }
}

+(void)showWithCurrentAlphaAnimation:(NSTimeInterval)time toEndAlpah:(CGFloat)alpha{
    if ([[UIApplication sharedApplication].keyWindow viewWithTag:10001]) {
        [[UIApplication sharedApplication].keyWindow addSubview:_keyWindowView];
        [UIView animateWithDuration:time animations:^{
            _keyWindowView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:alpha];
        }];
    }
}

-(void)addSubview:(UIView *)view{
    [super addSubview:view];
    NSLog(@"%@",NSStringFromCGRect(view.frame));
    if (view.size.width > 0) {
        //[view addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionOld context:nil];
        NSString * p =   [NSString stringWithFormat:@"%@",NSStringFromClass(view.class)];
        NSValue *value = [NSValue valueWithCGRect:view.frame];
        NSNumber *numbel = [NSNumber numberWithFloat:view.alpha];
        NSDictionary *dic = @{@"frame":value,@"alpha":numbel};
        [self.dicSubviews setValue:dic forKey:p];
    }

}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    //    NSLog(@"%p ---- %d");
    UIView *view = _keyWindowView.subviews.lastObject;
    NSString * p =   [NSString stringWithFormat:@"%p",_keyWindowView.subviews.lastObject];
    if ([self.dicSubviews objectForKey:p]) {
        return;
    }else{
        if ([keyPath isEqualToString:@"frame"]) {
            NSLog(@"%@",[change objectForKey:@"old"] );
            NSValue *value = [NSValue valueWithCGRect:view.frame];
            NSNumber *numbel = [NSNumber numberWithFloat:view.alpha];
            NSDictionary *dic = @{@"frame":value,@"alpha":numbel};
            [self.dicSubviews setValue:dic forKey:p];
        }
    }

}

-(void)willRemoveSubview:(UIView *)subview{
    NSString * p =   [NSString stringWithFormat:@"%p",&subview];
//    NSDictionary *dic = [self.dicSubviews objectForKey:p];
//    [UIView animateWithDuration:0.3 animations:^{
//        subview.frame = [[dic objectForKey:@"frame"] CGRectValue];
//        subview.alpha = [[dic objectForKey:@"alpha"] floatValue];
//    }];
//    [self.dicSubviews removeObjectForKey:p];
    [super willRemoveSubview:subview];
}

-(NSMutableDictionary *)dicSubviews{
    if (!_dicSubviews) {
        _dicSubviews = [[NSMutableDictionary alloc]init];
    }
    return _dicSubviews;
}

+(void)changeAlpha:(CGFloat)alpha{
    if (alpha > 0.8) {
        alpha = 0.8;
    }
    _keyWindowView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:alpha];
}

-(void)dealloc{

}
@end
